package csu.web.mypetstore.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import csu.web.mypetstore.pojo.Category;
import csu.web.mypetstore.pojo.Product;
import csu.web.mypetstore.service.CategoryService;
import csu.web.mypetstore.mapper.CategoryMapper;
import csu.web.mypetstore.service.ItemService;
import csu.web.mypetstore.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 18632
* @description 针对表【category】的数据库操作Service实现
* @createDate 2025-03-19 17:52:53
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private ProductService productService;

    @Autowired
    private ItemService itemService;

    @Override
    public void insertCategory(Category category) {
        categoryMapper.insert(category);
    }

    @Override
    public void updateCategory(Category category) {
        Wrapper<Category>wrapper = new LambdaQueryWrapper<Category>().eq(Category::getCatid, category.getCatid());
        categoryMapper.update(category, wrapper);
    }

    @Override
    public void deleteCategory(String categoryId) {
        List<Product> products = productService.getProductListByCategory(categoryId);
        for (Product product : products) {
            // 删除商品
            itemService.deleteItemInProduct(product.getProductid());
            // 删除产品
            productService.deleteProduct(product.getProductid());
        }
        Wrapper<Category>wrapper = new LambdaQueryWrapper<Category>().eq(Category::getCatid, categoryId);
        categoryMapper.delete(wrapper);

    }

    @Override
    public List<Category> getAllCategories() {



        return categoryMapper.selectList(null);
    }

    @Override
    public Category getCategory(String categoryId) {
        Wrapper<Category>wrapper = new LambdaQueryWrapper<Category>().eq(Category::getCatid, categoryId);
        return categoryMapper.selectOne(wrapper);
    }

}




